/*************************************************
 *FileName:      signin.js
 *Description:   签到.js
 *Others:        陈灿
 *************************************************/
    /*
     * 启动
     */
    mui.init();
    mui('.mui-scroll-wrapper').scroll({
        indicators: false,      //是否显示滚动条 默认为true
        deceleration: 0.003,    //阻尼系数,系数越小滑动越灵敏 默认0.0006
        bounce: false           //是否启用回弹 默认true
    });
    /*
     * 变量初始化
     */
    var uid = localStorage.replace_uid;
    var token = localStorage.replace_token;
    var log = false;
    var wk_day = '';
    var sign_card = 0;
    var retroactive = 0;
    /*
     * 主程序
     */ 
     mui.init({
        beforeback: function() {
            var list = plus.webview.currentWebview().opener();
            mui.fire(list, 'refreshindex');
            return true;
        }
    });
    
    $(document).ready(function() 
    {
        //样式加载
        pagecss();
    });
    /**
     * pagecss
     * 样式加载
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
    function pagecss(){
        $.ajax({
            type: "post",
            url: DOMAIN_NAME + "/index.php/api/user/signLogs",
            dataType:'jsonp',
            data: { uid: uid, token: token },
            success: function(data) {
                $('.signin-box > a').children('em').html('魅力+'+data.sign_exp);
                $('.signintext > p').eq(0).html('1.签到一天可获得'+data.sign_exp+'点魅力值。')
                $('.signintext > p').eq(1).html('2.连续签到7天，第7天可额外获得'+data.continuous_sign_exp+'点魅力值。（超过7天，经验奖励蒋进入下一个循环）')
                if(data.error_code == 0){
                    for(var i=0; i<data.data.length; i++){
                        var day = data.data[i].day - 1;
                        $('.signin-box > a').eq(day).addClass('active');
                        $('.signin-box > a').eq(day).children('em').html('魅力+'+data.data[i].exp);
                    }
                }
                $('.signin-box > a').each(function(){
                    var index = $(this).index();
                    if((index < (data.wk_day-1)) && !$(this).hasClass('active')){
                        $(this).children('em').html('补签需'+data.retroactive+'换币');
                    }
                })
                $('.signintext').html(data.sign_rules)
                retroactive = data.retroactive;
                sign_card = data.sign_card;
                wk_day = data.wk_day;
                log = true;
            }
        });
        
        $('.signin .mui-slider').each(function(){
            var firstSrc=$('.mui-slider-item:first-child').find('img').attr('src')
            var lastSrc=$('.mui-slider-item:last-child').find('img').attr('src');
            $('.mui-slider-item:first-child').before('<div class="mui-slider-item mui-slider-item-duplicate"><a href="#"><img src='+lastSrc+' /></a></div>');
            $('.mui-slider-item:last-child').after('<div class="mui-slider-item mui-slider-item-duplicate"><a href="#"><img src='+firstSrc+' /></a></div>');
        })//个人首页banner图片的环路以及下标
        $('.signin').on('tap','.signin-a',function(){
            if($(this).hasClass('active')){
                return false;
            }
            var day = $(this).index() + 1;
            var obj = $(this);
            if(day == wk_day){
                
                if(!($(this).hasClass('active')) && (log = true)){
                    sign(obj,day);
                }
            }else if(day < wk_day){
                if(sign_card){
                    mui.confirm('您有补签卡:<em class="gift-bqk"></em> ×'+sign_card+'，<br/>是否使用？','提示',['使用','取消'],function(e){
                        if(e.index == 0){
                            var stet=true;
                            sign(obj,day,stet);
                        }
                    },'div');
                }else{
                    mui.confirm('补签需要换币:<em class="gift-jb"></em> ×'+retroactive+'，<br/>是否支付？','提示',['支付','取消'],function(e){
                        if(e.index == 0){
                            sign(obj,day,false);
                        }
                    },'div');
                }  
            }else{
                mui.toast('还不能签到！',{ duration:2000, type:'div' });
            }
                
        })
    }

    function sign(obj,day,stet){
        $.ajax({
            type: "post",
            url: DOMAIN_NAME + "/index.php/api/user/sign",
            dataType:'jsonp',
            data: { uid: uid, token: token, day: day },
            success: function(data) {
                if(data.error_code == 0){
                    obj.addClass('active');
                    obj.children('em').html('魅力+'+data.exp);
                    mui.toast('签到成功！',{ duration:2000, type:'div' });
                    if(stet){
                        sign_card--;
                    }
                }else{
                    mui.toast(data.error_msg,{ duration:2000, type:'div' });
                }

            }
        });
    }
    /**
     * adds //方法名
     * @陈灿 //作者名
     * @DateTime 2018-01-010T0116:25:00+0800 //时间
     * @param    int    $goods_id //参数
     * @param    array  $data //参数
     * @return    array  [error_code, error_msg, goods_id] //返回值
     */